package com.blood.core.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author daoren
 */
public class DateUtil {
    private static long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
    private static long nh = 1000 * 60 * 60;// 一小时的毫秒数
    private static long nm = 1000 * 60;// 一分钟的毫秒数
    public static String defaultTime = "0小时";// 默认
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * @param startTime
     * @param endTime
     * @return
     */
    public static String dateDiff(Date startTime, Date endTime) {
        long hour;
        long min;
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        hour = diff / nh;// 计算差多少小时
        min = diff % nd % nh / nm;// 计算差多少分钟
        return (hour) + "小时"
                + (min) + "分钟";
    }

    /**
     * 计算时间相差几分钟
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long dateMinute(Date startTime, Date endTime) {
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long minute = diff / nm;// 计算差多少分钟
        return minute;
    }

    /**
     * 计算时间相差几小时
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long dateHour(Date startTime, Date endTime) {
        // 获得两个时间的毫秒时间差异
        long diff = endTime.getTime() - startTime.getTime();
        long hour = diff / nh;// 计算差多少小时
        return hour;
    }

    /**
     * 获取几小时后的时间
     *
     * @param hour 小时
     * @return
     */
    public static Date getAddHour(int hour, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取几天后的时间
     *
     * @param day 往后几天
     * @return
     */
    public static Date getAddDate(int day, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取几天前的时间
     *
     * @param day 往前几天
     * @return
     */
    public static Date getDownDayDate(int day, Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -day);
        date = calendar.getTime();
        return date;
    }

    /**
     * 得到day的起始时间点。
     *
     * @param date
     * @return
     */
    public static Date getDayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到day的终止时间点.
     *
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MILLISECOND, -1);
        return calendar.getTime();
    }

}
